import 'package:common_utils/common_utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/address_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_address_item_vo.dart';
import 'package:xinxuan/vo/system_address_list_vo.dart';

class AddressController extends GetxController {
  
  /// 选中的省份
  var providerItem = SystemAddressItem().obs;
  /// 选中的市
  var cityItem = SystemAddressItem().obs;
  /// 选中的区
  var areaItem = SystemAddressItem().obs;
  /// id 修改地址时不为空
  int id = 0;
  /// 省市区 地址
  var addressStr = ''.obs;
  /// 联系人
  var consignee = ''.obs;
  /// 手机号
  var mobile = ''.obs;
  /// 国号
  int country = 1;
  /// 省份ID
  int province = 0;
  /// 市ID
  int city = 0;
  /// 区ID
  int district = 0;
  /// 详细地址
  var address = ''.obs;
  /// 是否默认地址
  var isDefault = 1.obs;
  /// 地址列表
  var addressArr = <CommonAddressItem>[].obs;

  @override
  void onReady() {
    getAddressList();
    super.onReady();
  }
  
  /// 选中区时触发
  changeProviderAddress(SystemAddressItem item) {
    providerItem.value = item;
    cityItem.value = SystemAddressItem();
    areaItem.value = SystemAddressItem();
    addressStr.value = '';
    update();
  }

  /// 选中区时触发
  changeCityAddress(SystemAddressItem item) {
    cityItem.value = item;
    areaItem.value = SystemAddressItem();
    addressStr.value = '';
    update();
  }

  /// 选中区时触发
  changeAreaAddress(SystemAddressItem item) {
    areaItem.value = item;
    addressStr.value = '${providerItem.value.title} ${cityItem.value.title} ${areaItem.value.title}';
    update();
  }

  /// 初始化添加地址信息
  ininAddAddress() {
    consignee.value = '';
    mobile.value = '';
    province = 0;
    city = 0;
    district = 0;
    address.value = '';
    isDefault.value = 1;
    addressStr.value = '';
    providerItem.value = SystemAddressItem();
    cityItem.value = SystemAddressItem();
    areaItem.value = SystemAddressItem();
  }

  /// 初始化修改地址信息
  ininEditAddress(Map<String, dynamic> map) {
    id = map['id'];
    consignee.value = map['consignee'];
    mobile.value = map['mobile'];
    province = map['province'];
    city = map['city'];
    district = map['district'];
    address.value = map['address'];
    isDefault.value = map['isDefault'];
    addressStr.value = '${map['provinceStr']} ${map['cityStr']} ${map['districtStr']}';
    providerItem.value = SystemAddressItem(
      id: province,
      title: map['provinceStr']
    );
    cityItem.value = SystemAddressItem(
      id: city,
      title: map['cityStr']
    );
    areaItem.value = SystemAddressItem(
      id: district,
      title: map['districtStr']
    );
  }

  /// 获取地址列表
  getAddressList() async {
    AddressListVo addressListVo = await HttpUtilService.getAddressList();
    addressArr.value = addressListVo.data!;
    update();
  }

  /// 添加地址
  addAddress() async {
    if (!checkParam()) {
      return;
    }
    Map<String, dynamic> map = _assembleParam();
    bool res = await HttpUtilService.addAddress(map: map);
    if (res) {
      Get.back();
      EasyLoading.showToast('添加成功');
      getAddressList();
    }
  }

  /// 编辑地址
  editAddress() async {
    if (!checkParam()) {
      return;
    }
    Map<String, dynamic> map = _assembleParam();
    var res = await HttpUtilService.editAddress(id: id, map: map);
    if (res) {
      Get.back();
      EasyLoading.showToast('编辑成功');
      getAddressList();
    }
  }

  /// 组装参数
  Map<String, dynamic> _assembleParam() {
    province = providerItem.value.id!;
    city = cityItem.value.id!;
    district = areaItem.value.id!;
    Map<String, dynamic> map = {
      'consignee': consignee.value,
      'mobile': mobile.value,
      'country': country,
      'province': province,
      'city': city,
      'district': district,
      'address': address.value,
      'is_default': isDefault.value,
    };
    return map;
  }

  /// 检查参数
  checkParam() {
    if (TextUtil.isEmpty(consignee.value)) {
      EasyLoading.showToast('请填写收货人');
      return false;
    }
    if (!RegexUtil.isMobileExact(mobile.value)) {
      EasyLoading.showToast('请填写正确手机号');
      return false;
    }
    if (providerItem.value.id == null || cityItem.value.id == null || areaItem.value.id == null) {
      EasyLoading.showToast('请选择省市区');
      return false;
    }
    if (TextUtil.isEmpty(address.value)) {
      EasyLoading.showToast('请填写详细地址');
      return false;
    }
    return true;
  }

  /// 删除地址
  delAddress() {
    Get.dialog(DelDialog(message: '确定删除此地址吗？', onTap: () async {
      var res = await HttpUtilService.delAddress(id: id);
      if (res) {
        Get.back();
        EasyLoading.showToast('删除成功');
        getAddressList();
      }
    }));
  }

}